package net.lgpage.fragment;

import java.io.File;

import net.lgpage.util.FileIntentUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

public abstract class AbsSelectPhotoFileFragment extends BaseFragment{

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == TAKE_PHOTO) {
			if (resultCode == Activity.RESULT_OK) {
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 2;
				Bitmap filebitmap = BitmapFactory.decodeFile(
						photofile.getPath(), options);
				onTakePhotoBack(filebitmap);
			} else {
				onNothingSelect();
			}
		}else if(requestCode == SELECT_PHOTO){
			if (resultCode == Activity.RESULT_OK) {
				Uri uri = data.getData();
				String path = FileIntentUtil.getPath(getActivity(), uri);
				if (path != null && path.length() != 0) {
					if (FileIntentUtil.isImageFileType(path)) {
						photofile = new File(path);
						BitmapFactory.Options options = new BitmapFactory.Options();
						options.inSampleSize = 2;
						Bitmap filebitmap = BitmapFactory.decodeFile(
								photofile.getPath(), options);
						onTakePhotoBack(filebitmap);
					} else {
						onErrorSelect("文件类型不正确");
					}
				}else{
					onErrorSelect("路径为空");
				}
			} else {
				onNothingSelect();
			}
		}
	}
	public abstract void onFileBack(File file);
	public abstract void onNothingSelect();
	public abstract void onErrorSelect(String msg);
	public abstract void onTakePhotoBack(Bitmap bitmap);
	
	public static final int TAKE_PHOTO = 203;
	public static final int SELECT_PHOTO = 204;
	File photofile;
	
	public void takePhoto() {
		photofile = new File(Environment.getExternalStorageDirectory(),
				"temp.jpg");
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photofile));
		Activity ac = (Activity) getActivity();
		ac.startActivityForResult(intent, TAKE_PHOTO);
	}
	
	public File getPhotofile() {
		return photofile;
	}
	public void setPhotofile(File photofile) {
		this.photofile = photofile;
	}
	public void selectPhoto() {
		Activity ac = (Activity) getActivity();
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		try {
		ac.startActivityForResult(Intent.createChooser(intent, "选择一张图片"),
				SELECT_PHOTO);
		} catch (android.content.ActivityNotFoundException ex) {
			Toast.makeText(ac, "请安装一款图片选择器!", 1).show();
		}
	}
	
	public void alertSelectDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(
				getActivity());
		builder.setTitle("选择一种方式")
				.setNegativeButton("拍照上传",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(
									DialogInterface arg0,
									int arg1) {
								takePhoto();
							}
						})
				.setNeutralButton("文件选择",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(
									DialogInterface arg0,
									int arg1) {
								selectPhoto();
							}
						})
				.setPositiveButton("取消",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(
									DialogInterface dia,
									int arg1) {
								dia.cancel();
							}
						});
		builder.create().show();
	}
	
	
}
